<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        body{
            padding-bottom: 800px;
            padding-left: 30px;
        }
        .cbox{
            border: 1px solid #999;
            padding: 20px;
            margin: 10px 0px;
        }
    </style>
</head>
<body>
    <div id="app" class="cbox">
        <h4>顶层组件</h4>
        <comp-a @update:msg="updateMsg"></comp-a>
        <comp-b :msg="tempMsg"></comp-b>
    </div>
    <script type="module">
        // 加载 Vue3 的ESM 模块文件
        import { createApp } from "../../assets/vue3/vue.esm-browser.js"

        const CompAChild = {
            template:`
                <div class="cbox">
                    <h4>组件A-子组件</h4>
                    <p>msg:{{ msg }}</p>
                    <input type="button" value="发送MSG" @click="sendMsg()">
                </div>
            `,
            emits:["update:msg"],
            data() {
                return {
                    msg:"组件A的子组件仓库数据MSG"
                }
            },
            methods: {
                sendMsg() {
                    this.$emit("update:msg",this.msg)
                }
            },
        }

        const CompA = {
            components: {
                CompAChild,
            },
            template:`
                <div class="cbox">
                    <h4>组件A</h4>
                    <comp-a-child @update:msg="updateMsg"></comp-a-child>
                </div>
            `,
            emits:["update:msg"],
            methods: {
                updateMsg(arg) {
                    console.log("组件A的方法:",arg)
                    this.$emit("update:msg",arg)
                }
            },
        }

        const CompBChild = {
            template:`
                <div class="cbox">
                    <h4>组件B-子组件</h4>
                    <p>msg:{{ msg }}</p>
                </div>
            `,
            props:["msg"]
        }

        const CompB = {
            components: {
                CompBChild,
            },
            props:["msg"],
            template:`
                <div class="cbox">
                    <h4>组件B</h4>
                    <comp-b-child :msg="msg"></comp-b-child>
                </div>
            `
        }

        createApp({
            components: {
                CompA,CompB
            },
            data(){
                return {
                    tempMsg:""
                }
            },
            methods: {
                updateMsg(arg) {
                    console.log("根组件方法:",arg)
                    this.tempMsg = arg;
                }
            },
        }).mount("#app")
    </script>
</body>
</html>